<?php

namespace App\Http\Controllers\Supplier;

use App\Models\RechargeLog;
use App\Services\AlipayService;
use App\Services\CommonService;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
    /**
     * 获取我的账户余额
     * @param Request $request
     * @return false|string
     */
    public function myAccount(Request $request){
        $data['balance'] = $this->shopUser['shop_balance'];
        $data['frozen'] = $this->shopUser['frozen_amount'];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 获取收款账户信息
     * @param Request $request
     * @return false|string
     */
    public function getPayeeInfo(Request $request){
        $data['payeename'] = $this->shopUser['payeename'];
        $data['alipayaccount'] = $this->shopUser['alipayaccount'];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 账户充值
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
    public function recharge(Request $request){
        try{
            $amount = $request->input('amount','');
            if(!$amount){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $amount)) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'充值金额错误');
            }
            if($amount < RechargeLog::RECHARGE_AMOUNT_LIMIT){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'充值金额不能低于'.RechargeLog::RECHARGE_AMOUNT_LIMIT.'元');
            }
            $params = [
                'outTradeNo' => createNo(),
                'memberId' => $this->shopUser['member_id'],
                'totalAmount' => $amount,
                'subject' => '充值',
                'orderDescribe' => $this->shopUser['shopname'].'充值',
                'notifyUrl' => config('alipay.callbackDomain').CommonService::NOTICE_SUPPLIER_URL_RECHARGE,
            ];
            $res = AlipayService::alipayCodeScanningPayment($params);
            if($res['status']){
                $log = [
                    'out_trade_no'=>$params['outTradeNo'],
                    'member_id'=>$this->shopUser['member_id'],
                    'amount'=>$amount,
                    'request'=>json_encode($params),
                    'result'=>json_encode($res),
                    'create_time'=>dateFormat(time()),
                ];
                $logId = RechargeLog::insertGetId($log);
                if($logId){
                    $data = [
                        'out_trade_no'=>$res['data']['out_trade_no'],
                        'qr_code'=>$res['data']['qr_code'],
                        'log_id'=>$logId,
                    ];
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'唤起充值操作失败');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
        }

    }

    /**
     * 充值结果查询 只轮询3分钟
     * @param Request $request
     * @return false|string
     */
    public function rechargeQuery(Request $request){
        $rechargeId = $request->input('rechargeId','');
        if(!$rechargeId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $log = RechargeLog::where(['id'=>$rechargeId,'member_id'=>$this->shopUser['member_id']])->first();
        $log = obToArray($log);
        if(!$log){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到充值记录');
        }
        if($log['status'] == RechargeLog::STATUS_SUCCESS){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'充值成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL);
        }
    }
}
